import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/client/home/index.vue'
import Index from '../views/client/index/index.vue'

// import { useI18n } from 'vue-i18n'

const routes: Array<RouteRecordRaw> = [
      {
            path: '/coacest',
            name: 'index',
            component: Index,
            children: [
                  // 首页
                  {
                        path: '/home.html',
                        name: 'Home',
                        component: Home,
                        meta: { title: 'COACEST 京咛' } 
                  },
                  // 动态详情页
                  {
                        path: '/news/index.html',
                        name: 'news',
                        component: () => import(/* webpackChunkName: "news" */ '../views/client/news/index.vue'),
                        meta: { title: '新闻' }
                  },
                  // 简介
                  {
                        path: '/brief/introduction/index.html',
                        name: 'briefIntroduction',
                        component: () => import(/* webpackChunkName: "briefIntroduction" */ '../views/client/briefIntroduction/index.vue'),
                        meta: { title: '简介-COACEST京咛' }
                  },
                  // 业务
                  {
                        path: '/business/index.html',
                        name: 'business',
                        component: () => import(/* webpackChunkName: "business" */ '../views/client/business/index.vue'),
                        meta: { title: '业务-COACEST京咛' }
                  },
                  // 员工
                  {
                        path: '/staff/index.html',
                        name: 'staff',
                        component: () => import(/* webpackChunkName: "staff" */ '../views/client/staff/index.vue'),
                        meta: { title: '员工-COACEST京咛' }
                  },
                  // ESG
                  {
                        path: '/esg/index.html',
                        name: 'esg',
                        component: () => import(/* webpackChunkName: "esg" */ '../views/client/esg/index.vue'),
                        meta: { title: 'ESG-COACEST京咛' }
                  },
                  // 投资者
                  {
                        path: '/investor/index.html',
                        name: 'investor',
                        component: () => import(/* webpackChunkName: "investor" */ '../views/client/investor/index.vue'),
                        meta: { title: '投资者-COACEST京咛' }
                  },
                  // 媒体
                  {
                        path: '/medium/index.html',
                        name: 'medium',
                        component: () => import(/* webpackChunkName: "medium" */ '../views/client/medium/index.vue'),
                        meta: { title: '媒体-COACEST京咛' }
                  },
                  // 企业动态
                  {
                        path: '/medium/news.html',
                        name: 'mediumNews',
                        component: () => import(/* webpackChunkName: "mediumNews" */ '../views/client/medium/mediumNews.vue'),
                        meta: { title: '媒体-COACEST京咛' }
                  },
                  // 媒体资料库
                  {
                        path: '/medium/library.html',
                        name: 'library',
                        component: () => import(/* webpackChunkName: "library" */ '../views/client/medium/library.vue'),
                        meta: { title: '媒体-COACEST京咛' }
                  }
            ]
      },
      // error 统一状态码处理页
      {
            path: '/status/:catchAll(.*)/index.html',
            name: 'error',
            component: () => import(/* webpackChunkName: "error" */ '../views/error/index.vue'),
            meta: { title: '' }
      },
      // 维护中页面
      {
            path: '/stand/index.html',
            name: 'stand',
            component: () => import(/* webpackChunkName: "stand"*/ '../views/client/stand/index.vue'),
            meta: { title: '系统维护-COACEST京咛' }
      },
      // 管理后台登录
      {
            path: '/admin/index.html',
            name: 'admin',
            component: () => import(/* webpackChunkName: "admin" */ '../views/admin/index/index.vue'),
            meta: { title: '登录-COACEST京咛' }
      },
      // 管理后台登录
      {
            path: '/admin/home/index.html',
            name: 'adminHome',
            component: () => import(/* webpackChunkName: "adminHome" */ '../views/admin/home/index.vue'),
            children: [
                  {
                        path: '/admin/news/index.html',
                        name: 'adminNews',
                        component: () => import(/* webpackChunkName: "adminNews" */ '../views/admin/news/index.vue'),
                        meta: { title: '新闻动态-COACEST京咛' }
                  }
            ]
      }
]

const router = createRouter({
      history: createWebHistory(process.env.BASE_URL),
      routes
})

// 导航守卫 => 路由跳转前执行的操作
router.beforeEach((to, form, next) => {
      console.log('当前路由:=>', to.path)
      let doc_title = to.meta.title as string
      let boolStand = false
      if (to.path == '/') {
            if (boolStand) {
                  next({ path: '/stand/index.html' }) 
            } else {
                  setTimeout(() => {
                        next({ path: '/home.html' })
                  }, 100)
                  return
            }
      } else {
            if(to.matched.length == 0) {
                  if(!form.name) {
                        next({ path: '/status/404/index.html', replace: true })
                  }
            } else {
                  next()
            }
      }
      window.document.title = doc_title
})

export default router
